<template>
  <div class="page-content">
    <el-card>
      <el-row  style="margin-bottom: 10px" >
        <el-col :span="2" :offset="22"
          ><el-button type="primary" @click="addDialog = true"
          v-auth="'basics_authority_insert'"
            >添加角色</el-button
          ></el-col
        >
      </el-row>
      <TheTable
        :data="tableDataList"
        :page.sync="page"
        :showPage="showPage"
        @changeSize="changeSize"
        @changePage="changePage"
      >
        <el-table-column label="角色标签" prop="name"></el-table-column>
        <el-table-column label="拥有权限">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-search"
              v-auth="'basics_authority_select'"
              @click="viewPermissions(scope.row.id)"
              >查看权限</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="success"
              icon="el-icon-setting"
              v-auth="'basics_authority_update'"
              @click="setAuth(scope.row)"
              >分配权限</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-edit"
              v-auth="'basics_authority_delete'"
              @click="remove(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </TheTable>
      <RoleComponent
        :addDialog="addDialog"
        @closeAdd="closeAdd"
      ></RoleComponent>
      <!-- 查看角色权限对话框 -->
      <Authority
        :roleId="roleId"
        :authorityVisible="authorityVisible"
        @closeAuthority="closeAuthority"
      />
      <!-- 分配权限 -->
      <AssignPermissions
        :editObj="editObj"
        :drawerVisible="drawerVisible"
        @fatherMethod="drawerVisible = false"
      ></AssignPermissions>
          <!-- 所持权限 -->
    <personal-authority @close="permissionVisible = false" :permissionVisible="permissionVisible"/>
    </el-card>
  </div>
</template>

<script>
import {
  getAllRoleByPage,
  deleteRole,
  editRole,
} from "../../Api/rightsmanage/index";
import RoleComponent from "../../components/RightsManagement/RoleComponent";
import Authority from "../../components/RightsManagement/ToAllocate/Authority";
import PersonalAuthority from "../../components/RightsManagement/PersonalAuthority"
import AssignPermissions from "../../components/RightsManagement/ToAllocate/AssignPermissions";
export default {
  name: "ToAllocate",
  data() {
    return {
      msg: "权限管理--权限分配",
      tableDataList: [],
      showPage: true,
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
      },
      columns: [
        { name: "角色标签", show: true, prop: "name" },
        //   { name: "权级", show: true, prop: "weight" },
        //   { name: "标记", show: true, prop: "remarks" },
        //   { name: "是否在职", show: true, prop: "usable" },
      ],
      addDialog: false, //添加角色对话框,
      roleId: "",
      authorityVisible: false,
      //分配权限树
      drawerVisible: false,
      //分配角色对象
      editObj: {},
      //所持权限
      permissionVisible:false
    };
  },
  components: {
    RoleComponent,
    Authority,
    AssignPermissions,
    PersonalAuthority
  },
  created() {
    this.getTableData();
    this.init();
  },
  methods: {
    init(){
      if(this.$route.query.showPermission){
        this.permissionVisible = true;
      }
    },
    async getTableData() {
      const { data: res } = await getAllRoleByPage(
        this.page.pageSize,
        this.page.current
      );
      console.log(res);
      if (res.statusCode === 20000) {
        this.tableDataList = res.data.records;
        this.page.total = res.data.total;
      }
    },
    changeSize(size) {
      this.page.pageSize = size.pageSize;
      this.getTableData();
    },
    changePage(page) {
      this.page.current = page.current;
      this.getTableData();
    },
    closeAdd() {
      this.addDialog = false;
    },
    closeAuthority() {
      this.authorityVisible = false;
    },
    async remove(id) {
      console.log(id);
      const confirmResuly = await this.$confirm(
        "此操作将永久删除所选角色信息, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => {
        return err;
      });
      if (confirmResuly !== "confirm") {
        return this.$message.info("已取消删除");
      }
      const {data: res} = await deleteRole(id)
      if(res.statusCode === 20000){
        this.$message.success('删除成功');
      }
    },
    viewPermissions(id) {
      this.roleId = id;
      this.authorityVisible = true;
    },
    setAuth(row) {
      this.editObj.id = row.id;
      this.editObj.name = row.name;
      this.editObj.remarks = row.remarks;
      this.drawerVisible = true;
    },
  },
};
</script>